<?php

namespace app\admin\controller\clothmaking_plan;

use app\common\controller\Backend;

/**
 * 生产计划
 *
 * @icon fa fa-circle-o
 */
class Index extends Backend
{

    /**
     * Index模型对象
     * @var \app\admin\model\clothmaking_plan\Index
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\clothmaking_plan\Index;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("producttypelist", $this->model->getTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {

        $$orderproduct_id = input('get.orderproduct_id', 0);
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['product', 'recipes'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->view->assign("orderproduct_id", $orderproduct_id);
        return $this->view->fetch();
    }

    public function selectpage()
    {
        // 设置分页参数
        $page = $this->request->get('pageNumber', 1); // 当前页码
        $pageSize = $this->request->get('pageSize', 10); // 每页条数
        $search = $this->request->get('search', ''); // 搜索关键字
        // 自定义搜索条件
        $custom = (array) $this->request->request("custom/a");
        // 查询条件
        $where = [];
        if ($search) {
            $where[] = ['title', 'like', '%' . $search . '%']; // 根据 title 字段搜索
        }
        // 添加自定义搜索条件
        foreach ($custom as $k => $v) {
            if (is_array($v) && count($v) == 2) {
                $where[$k] = [$v[0], $v[1]];
            } else {
                $where[$k] = $v;
            }
        }

        // 查询数据
        $list = $this->model // 替换为你的表名
            ->where($where)->with(['productBind'])

            ->page($page, $pageSize)
            ->select();

        // 查询总数
        $total = $this->model->where($where)
            ->count();

        // 返回数据
        return json([
            'list' => $list, // 数据列表
            'total' => $total, // 总条数
        ]);
    }

    public function add()
    {
        $alias = '';
        $quantity = 0;
        $productid = 0;
        $orderproduct_id = input('get.orderproduct_id', 0);
        $orderProduct = \app\admin\model\ClothmakingOrdersProduct::find($orderproduct_id);
        if ($orderProduct) {
            $alias = $orderProduct->product->alias;
            $productid = $orderProduct->product_id;
            $quantity = $orderProduct->quantity;
        }
        $this->view->assign("alias", $alias);
        $this->view->assign("quantity", $quantity);
        $this->view->assign("product_id", $productid);
        $this->view->assign("orderproduct_id", $orderproduct_id);

        return parent::add();
    }

    public function del($ids = null)
    {
        if ($ids) {

            $list = $this->model->where('id', 'in', $ids)->where('status', '<>', 'completed')->select();
            $count = 0;
            foreach ($list as $k => $v) {
                $count += $v->delete();
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
    }
}
